{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of Contours found : 10\n",
      "[[[396 134]]\n",
      "\n",
      " [[270 139]]\n",
      "\n",
      " [[270 173]]\n",
      "\n",
      " [[395 167]]]\n"
     ]
    }
   ],
   "source": [
    "#programming_fever\n",
    "#number plate detection\n",
    "import cv2\n",
    "import imutils as im\n",
    "\n",
    "input = 'E://OpenCV//car1.jpg'\n",
    "image = cv2.imread(input)\n",
    "\n",
    "newwidth = 500\n",
    "image = im.resize(image, width=newwidth)\n",
    "\n",
    "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "d, sigmaColor, sigmaSpace = 11,17,17\n",
    "filtered_img = cv2.bilateralFilter(gray, d, sigmaColor, sigmaSpace)\n",
    "\n",
    "lower, upper = 170, 200\n",
    "edged = cv2.Canny(filtered_img, lower, upper)\n",
    "\n",
    "\n",
    "cnts,hir = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n",
    "\n",
    "cnts=sorted(cnts, key = cv2.contourArea, reverse = True)[:10]\n",
    "NumberPlateCnt = None\n",
    "print(\"Number of Contours found : \" + str(len(cnts)))\n",
    "\n",
    "count = 0\n",
    "for c in cnts:\n",
    "        peri = cv2.arcLength(c, True)\n",
    "        epsilon = 0.01 * peri\n",
    "        approx = cv2.approxPolyDP(c, epsilon, True)\n",
    "        if len(approx) == 4:  \n",
    "            print(approx)\n",
    "            NumberPlateCnt = approx \n",
    "            break\n",
    "\n",
    "\n",
    "cv2.imshow(\"Input Image\", image)\n",
    "cv2.imshow(\"Gray scale Image\", gray)\n",
    "cv2.imshow(\"After Applying Bilateral Filter\", filtered_img)\n",
    "cv2.imshow(\"After Canny Edges\", edged)\n",
    "\n",
    "cv2.drawContours(image, [NumberPlateCnt], -1, (255,0,0), 2)\n",
    "cv2.imshow(\"Output\", image)\n",
    "\n",
    "cv2.waitKey(0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
